<?php

/**
 * kcDoctrineTable
 *
 * @package    flexiwik
 * @subpackage lib.model
 * @author     Jonathan Olger Nieto Lajo <jonathan_nieto@hotmail.com>
 */
class kcDoctrineTable extends Doctrine_Table
{
  public function findBySlugs($slugs)
  {
    $q = $this->createQuery()
         ->whereIn($this->getAlias().'.slug', $slugs);
    
    return $q->execute();
  }
  public function deleteBySlugs($slugs)
  {
    $column  = $this->hasColumn('slug') ? 'slug' : 'id';
    $deletes = $this->createQuery()
               ->delete()
               ->whereIn($column, $slugs)
               ->execute();
               
    return $deletes;
  }
  public function deleteAll()
  {
    $deletes = $this->createQuery()
               ->delete()
               ->execute();
               
    return $deletes;
  }
  
  /**
   * createQuery
   * creates a new kcDoctrineQuery object and adds the component name
   * of this table as the query 'from' part
   *
   * @param string Optional alias name for component aliasing.
   * @return kcDoctrineQuery
   */
  public function createQuery($alias = '')
  {
    $alias = empty($alias) ? $this->getAlias() : trim($alias);
    $alias = ' '.$alias;
    
    return kcDoctrineQuery::create($this->_conn)->from($this->getComponentName().$alias);
  }
  public function getAlias()
  {
    $name = get_class($this);
    return strtolower($name{0});
  }
  
  public function getNextId()
  {
  	$result = Doctrine_Manager::getInstance()
  	          ->getCurrentConnection()
  	          ->fetchAssoc(sprintf('SHOW TABLE STATUS LIKE \'%s\'', $this->getTableName()));
  	
    return $result[0]['Auto_increment'];
  }
}
